public class Block extends Entity
{
  private int factor;
  private boolean[][] grid;
  
  public Block(int xpos, int ypos)
  {
    this.xpos = xpos;
    this.ypos = ypos;
    this.renderSizeX = 100;
    this.renderSizeY = 60;
    this.factor = 5;
    
    this.grid = new boolean[renderSizeX / factor][renderSizeY / factor];
    
    for(int x = 0; x < renderSizeX / factor; x++)
    {
      for(int y = 0; y < renderSizeY / factor; y++)
      {
        if (!(x >= 5 && x <= 15 && y >= 7))
        {
          grid[x][y] = true;
        }
      }
    }
  }

  public void updatePosition(AnimationContext context)
  {
  }

  public boolean isOutsideZone()
  {
    return false;
  }

  boolean checkCollision(Entity entity)
  {
    return false;
  }
  
  boolean intersects(Entity bomb, boolean fromTop)
  {
    for(int x = 0; x < renderSizeX / factor; x++)
    {
      for(int y = 0; y < renderSizeY / factor; y++)
      {
        if (grid[x][y])
        {
          int x2 = xpos + x * factor;
          int y2 = ypos + y * factor;
          
          boolean hit = (x2 + factor >= bomb.xpos && x2 <= bomb.xpos + bomb.renderSizeX) &&
                        (y2 + factor >= bomb.ypos && y2 <= bomb.ypos + bomb.renderSizeY);
                     
          if (hit)
          {
            grid[x][y] = false;
            if (fromTop)
            {
              disableBlock(x - 3, y + 1);
            }
            else
            {
              disableBlock(x - 3, y - 4);
            }
            return true;
          }
        }
      }
    }
    
    return false;
  }
  
  private void disableBlock(int x, int y)
  {
    for(int i = x; i < x + 7; i++)
    {
      for(int j = y; j < y + 5; j++)
      {
        if (i >= 0 && i < renderSizeX / factor && j >= 0 && j < renderSizeY / factor)
        {
          if (int(random(0,2)) == 1)
          {
            grid[i][j] = false;
          }
        }
      }
    }
  }
  
  public void display()
  {
    fill(#00FF00, 255);
    for(int x = 0; x < renderSizeX / factor; x++)
    {
      for(int y = 0; y < renderSizeY / factor; y++)
      {
        if (grid[x][y])
        {
          rect(xpos + x * factor, ypos + y * factor, factor, factor);
        }
      }
    }
  }
}

